#include "init.h"

#define US_COUNT    (SystemCoreClock / 8000000)
#define MS_COUNT    (1000 * US_COUNT)

void delayInit()
{
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
}

void delayUs(u32 nus)
{
	u32 temp;
  SysTick->LOAD = nus * US_COUNT;  
  SysTick->VAL = 0x00;                       
  SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ; 
  do
  {
		temp = SysTick->CTRL;
  }
	while((temp & 0x01) && (!(temp & (1 << 16))));    
  SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; 
  SysTick->VAL = 0X00;                      
}

void delayMs(u16 nms)
{
	u32 temp;
	SysTick->LOAD =(u32)nms * MS_COUNT;
	SysTick->VAL = 0x00;
	SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
	do
	{
		temp = SysTick->CTRL;
	}
	while((temp & 0x01) && (!(temp & (1 << 16))));
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
	SysTick->VAL = 0X00;
}

void ParticularDelayMs(u16 nms)
{
	u32 temp;
	SysTick->LOAD =(u32)nms * MS_COUNT;
	SysTick->VAL = 0x00;
	SysTick->CTRL|= SysTick_CTRL_ENABLE_Msk;
	do
	{
		temp=SysTick->CTRL;
	}
	while((temp & 0x01)&&!(temp & (1<<16)));
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
	SysTick->VAL = 0X00;
}
